package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.web.response.Resp;
import com.oa.pm.dto.LearningExperienceSaveDTO;
import com.oa.pm.entity.LearningExperience;
import com.oa.pm.entity.LearningExperienceFile;
import com.oa.pm.mapper.LearningExperienceMapper;
import com.oa.pm.service.ILearningExperienceFileService;
import com.oa.pm.service.ILearningExperienceService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 学习经历表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class LearningExperienceServiceImpl extends ServiceImpl<LearningExperienceMapper, LearningExperience> implements ILearningExperienceService {

    private final LearningExperienceMapper learningExperienceMapper;

    @Autowired
    public LearningExperienceServiceImpl(LearningExperienceMapper learningExperienceMapper) {
        this.learningExperienceMapper = learningExperienceMapper;
    }

    @Autowired
    private ILearningExperienceFileService learningExperienceFileService;

    @Override
    public Resp save(LearningExperienceSaveDTO learningExperienceSaveDTO) {
        if(insertOrUpdate(learningExperienceSaveDTO.getLearningExperience())){
            learningExperienceSaveDTO.getLearningExperienceFile().setLearningExperienceId(learningExperienceSaveDTO.getLearningExperience().getId());
            if(learningExperienceFileService.insertOrUpdate(learningExperienceSaveDTO.getLearningExperienceFile())){
                return new Resp().success();
            }
        }
        return new Resp().failure();
    }

    @Override
    public LearningExperienceSaveDTO findById(String id) {
        LearningExperienceSaveDTO learningExperienceSaveDTO = new LearningExperienceSaveDTO();
        learningExperienceSaveDTO.setLearningExperience(selectById(id));
        learningExperienceSaveDTO.setLearningExperienceFile(learningExperienceFileService.selectOne(new EntityWrapper<LearningExperienceFile>().eq("learning_experience_id", id)));
        return learningExperienceSaveDTO;
    }
}
